﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>1.0.User表的增删改</title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>自定义视图数据</h3>
    <b>
        1.视图数据的生成
    </b>
    <pre class="brush: csharp;">
        public class IndexController:BaseController
        {
            //a.利用ViewBagMember属性添加视图数据
            [ViewBagMember]//把该参数添加到视图数据中
		    [Newtonsoft.Json.JsonIgnore]//转Json时忽略该参数
		    public string imageServerUrl;

            public void Index()
            {
                //b.利用dynamic自动添加视图数据
                this.ViewBag.a=1;
                this.ViewBag.b="Hello!";
            }
        }
        //当你保存以上控制器时，VS会自动在该类下生成IndexController.model.cs类
        public class Default
	    {
            [ViewBagMember]
		    [Newtonsoft.Json.JsonIgnore]
		    public string imageServerUrl;
		    [ViewBagMember]
		    [Newtonsoft.Json.JsonIgnore]
		    public NFinal.Collections.FastSearch.FastSearch&lt;NFinal.StringContainer> systemConfig;
		    public int a;
		    public string b;
	    }
    </pre>
    <p>
        2.把视图数据转为Json：
    </p>
    <pre class="brush: csharp">
        public class IndexController:BaseController
        {
            public void Index()
            {
                this.ViewBag.a=1;
                this.ViewBag.b="Hello!";
                //输出结果为：{"a":1,"b":"Hello!"}
                this.AjaxReturn();
            }
        }
    </pre>
    <p>
        3.把视图数据传到模板中:
    </p>
    <pre class="brush: csharp;">
        public class IndexController:BaseController
        {
            public void Index()
            {
                this.ViewBag.a=1;
                this.ViewBag.b="Hello!";
                //默认视图路径为："项目名/Views/Index/Index.cshtml"
                this.Render();
            }
        }
    </pre>
</body>
</html>